package com.h.forum.pojo.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.h.forum.common.enums.ResultCode;
import lombok.Data;

/**
 * 统一返回结果处理
 * @param <T>
 */
@Data
public class AppResult<T> {

    // 状态码
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private int code;

    // 消息
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private String message;

    // 数据
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private T data;

    public AppResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功
     */
    public static AppResult success() {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static AppResult success(String message) {
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }

    public static <T> AppResult<T> success(T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    public static <T> AppResult<T> success(String message,T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message,data);
    }

    /**
     * 失败
     */
    public static AppResult fail() {
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    public static AppResult fail(String message) {
        return new AppResult(ResultCode.FAILED.getCode(), message);
    }

    public static  AppResult fail(ResultCode code) {
        return new AppResult(code.getCode(), code.getMessage());
    }


}
